package com.googlecode.stockhamster.bank;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Check {
	private String username;
	private String moneyCode;
	private long money;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public static Check get(String moneyCode) {
		Check tempAccount = new Check();
		try {
			String sqlGetAccount = "select * from BankCheck where moneyCode=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setString(1, moneyCode);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempAccount.setUsername(rs.getString(1));
			tempAccount.setMoneyCode(rs.getString(2));
			tempAccount.setMoney(rs.getLong(3));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return tempAccount;
	}

	public static Check[] getAll() {
		Check[] targetList = new Check[0];
		try {
			List<Check> tempAccount = new ArrayList<Check>();
			String sqlGetAccountList = "select * from BankCheck";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			rs = state.executeQuery();
			while (rs.next()) {
				Check ac = new Check();
				ac.setUsername(rs.getString(1));
				ac.setMoneyCode(rs.getString(3));
				ac.setMoney(rs.getLong(3));
				tempAccount.add(ac);
			}
			targetList = new Check[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}

	public boolean save() {
		boolean flag = false;
		try {
			String sqlSetArbiterAccount = "update BankCheck set money=?, moneyCode=? where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlSetArbiterAccount);
			state.setString(3, getUsername());
			state.setString(2, getMoneyCode());
			state.setLong(1, getMoney());
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public boolean insert() {
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into BankCheck(username, moneyCode, money) values(?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(sqlAddAmount);
			state.setString(1, getUsername());
			state.setString(2, getMoneyCode());
			state.setLong(3, getMoney());
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public boolean delete() {
		boolean flag = false;
		try {
			String sqlDelAccount = "delete from BankCheck where username=? and moneyCode=?";
			state = ConnectDatabase.getConnection().prepareStatement(sqlDelAccount);
			state.setString(1, username);
			state.setString(2, moneyCode);
			state.executeUpdate();
			flag = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMoneyCode() {
		return moneyCode;
	}

	public void setMoneyCode(String moneyCode) {
		this.moneyCode = moneyCode;
	}

	public long getMoney() {
		return money;
	}

	public void setMoney(Long money) {
		this.money = money;
	}

}
